package run.siyuan.jdk.测试;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: ApiTest
 * @Description: TODO Java 各种 api 测试
 * @author: siyuan
 * @date: 2022/3/11 5:29 PM
 */
public class ApiTest {

    String string = "{\"stackTrace\":[\"com.central.mall.mq.ServiceConsumer.consumer(ServiceConsumer.java:32)\",\"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\",\"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\",\"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\",\"java.lang.reflect.Method.invoke(Method.java:498)\",\"org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171)\",\"org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120)\",\"org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:53)\",\"org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:220)\",\"org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:148)\",\"org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:133)\",\"org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1579)\",\"org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1498)\",\"org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1486)\",\"org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1477)\",\"org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1421)\",\"org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:963)\",\"org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:913)\",\"org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:81)\",\"org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1284)\",\"org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1190)\",\"java.lang.Thread.run(Thread.java:748)\"]}";


    @Data
    public class Obj{
        private Integer id;
        private String errMsg;

        @Override
        public String toString() {
            return "Obj{" +
                    "id=" + id +
                    ", errMsg='" + errMsg + '\'' +
                    '}';
        }
    }

    /**
     * @date: 2022/4/20
     * @description: TODO DateUtil.between
     * @param:
     * @return: void
     */
    @Test
    public void test10() throws InterruptedException {
        String jsonStr = JSONUtil.toJsonStr(string);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("id",123);
        jsonObject.set("errMsg",jsonStr);
        Obj bean = JSONUtil.toBean(jsonObject, Obj.class);
        System.out.println(bean.toString());


    }

    /**
     * @date: 2022/4/20
     * @description: TODO DateUtil.between
     * @param:
     * @return: void
     */
    @Test
    public void test9() throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000 * 8);
            System.out.println(System.currentTimeMillis());
        }


    }

    /**
     * @date: 2022/4/20
     * @description: TODO DateUtil.between
     * @param:
     * @return: void
     */
    @Test
    public void test8() {
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(1L);
        longs.add(8L);
        String join = StrUtil.join(",", longs);
        System.out.println(join);

    }


    /**
     * @date: 2022/4/20
     * @description: TODO DateUtil.between
     * @param:
     * @return: void
     */
    @Test
    public void test7() {
//        DateTime dateTime1 = DateUtil.parseDateTime("2022-04-22 00:00:00");
//        DateTime dateTime2 = DateUtil.parseDateTime("2022-04-20 00:00:01");
//        System.out.println(DateUtil.between(dateTime1,dateTime2, DateUnit.DAY));
        String a = (String)null;
        System.out.println(a);
    }

    /**
     * @date: 2022/3/11
     * @description: TODO ListUtil.split list 合并
     * @param:
     * @return: void
     */
    @Test
    public void test6() {
        System.out.println("numLimit".endsWith("Limit"));
        System.out.println(DateUtil.offsetMonth(DateUtil.date(),-6).getTime());
        System.out.println(System.currentTimeMillis());
        System.out.println(DateUtil.formatDateTime(DateUtil.date(1635419466650L)) +"   "+ DateUtil.formatDateTime(DateUtil.date(1651144266666L)));
    }

    /**
     * @date: 2022/3/11
     * @description: TODO ListUtil.split list 合并
     * @param:
     * @return: void
     */
    @Test
    public void test5() throws ParserConfigurationException, IOException, SAXException {
        FileInputStream inputStream = new FileInputStream("/Users/siyuan/IdeaProjects/siyuan/siyuan-jdk-server/siyuan-jdk-center/pom.xml");
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(inputStream);
        System.out.println(document.toString());

    }

    /**
     * @date: 2022/3/11
     * @description: TODO ListUtil.split list 合并
     * @param:
     * @return: void
     */
    @Test
    public void test4(){
        ArrayList<String> list = new ArrayList<>();
        list.add("add1");
        list.add("add2");
        list.add("add3");
        list.add("add4");
        list.add("add5");
        List<List<String>> split = ListUtil.split(list, 10);
        System.out.println(split.toString());

    }

    /**
     * @date: 2022/3/11
     * @description: TODO Map containsKey
     * @param:
     * @return: void
     */
    @Test
    public void test3(){
        DateTime dateTime = DateUtil.parseDate("2022-03-16 00:00:00");
        String format = DateUtil.format(dateTime, DatePattern.NORM_DATE_PATTERN);
        String format1 = DateUtil.format(DateUtil.date(System.currentTimeMillis()), DatePattern.NORM_DATE_PATTERN);
        System.out.println(format.compareTo(format1));

    }

    /**
     * @date: 2022/3/11
     * @description: TODO Map containsKey
     * @param:
     * @return: void
     */
    @Test
    public void test2(){
        DateTime dateTime = DateUtil.parseDate("2022-03-14 00:00:00");
        System.out.println(dateTime.getTime());
        System.out.println(System.currentTimeMillis());
        long time = DateUtil.offsetDay(dateTime, 2).getTime();
        System.out.println(time);
        System.out.println(time - dateTime.getTime());
        System.out.println(1000 * 60 * 60 *24 * 2);
        System.out.println(time - dateTime.getTime() > 1000 * 60 *60 * 24 * 2);
    }

    /**
    * @date: 2022/3/11
    * @description: TODO Map containsKey
    * @param:
    * @return: void
    */
    @Test
    public void test1(){
        Map<String, Object> map = new HashMap<>();
        map.put("123",1);
        map.put("345",0);
        System.out.println(map.toString());
        int i = map.containsKey("234") ? 1 : 0;
        System.out.println(i);
        i = map.containsKey("123") ? 1 : 0;
        System.out.println(i);
    }

}
